import 'package:flutter/services.dart';

class Preferences {
  // 创建一个静态的私有实例
  static final Preferences _instance = Preferences._internal();

  // 通信通道
  // ignore: non_constant_identifier_names
  static final String _CHANNEL = 'preferences';

  // Flutter与原生Android代码通信的MethodChannel实例
  static final MethodChannel _channel = MethodChannel(_CHANNEL);

  // 私有构造函数
  Preferences._internal();

  // 获取单例实例
  static Preferences get instance => _instance;

  // 獲取開場白文件的Uri
  static Future<String?> getOpeningStatement() async {
    try {
      return await _channel.invokeMethod('getOpeningStatement');
    } on PlatformException catch (e) {
      print("获取开场白失败: ${e.message}");
      return null;
    }
  }

  // 獲取答录机的狀態
  static Future<bool> isAnsweringMachineEnabled() async {
    try {
      final bool isEnabled = await _channel.invokeMethod('getEnabled');
      return isEnabled;
    } on PlatformException catch (e) {
      print("获取答录机状态失败: ${e.message}");
      return false;
    }
  }

  // 設置答錄機狀態
  static Future<void> setAnsweringMachineEnabled(bool enabled) async {
    try {
      await _channel.invokeMethod('setEnabled', enabled);
    } on PlatformException catch (e) {
      print("设置答录机状态失败: ${e.message}");
    }
  }

  // 獲取錄音功能狀態
  static Future<bool> isRecordingEnabled() async {
    try {
      final bool isEnabled = await _channel.invokeMethod('getRecordingEnabled');
      return isEnabled;
    } on PlatformException catch (e) {
      print("获取录音功能状态失败: ${e.message}");
      return false;
    }
  }

  // 設置錄音功能狀態
  static Future<void> setRecordingEnabled(bool enabled) async {
    try {
      await _channel.invokeMethod('setRecordingEnabled', enabled);
    } on PlatformException catch (e) {
      print("设置录音功能状态失败: ${e.message}");
    }
  }

  // 獲取錄音文件的Uri
  static Future<String?> getRecordingOutputFile() async {
    try {
      return await _channel.invokeMethod('getRecordingOutputFile');
    } on PlatformException catch (e) {
      print("获取录音文件失败: ${e.message}");
      return null;
    }
  }
}
